import Router from "vue-router"
import Vue from "vue"
Vue.use(Router)


const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  routes:[{
    path: "/index",
    name:"index",
    component: ()=> import("../page/index/index.vue"),
    children:[{
      path:"home",
      component: ()=> import("../page/home/home.vue")
    },{
      path:"sort",
      component: ()=> import("../page/sort/sort.vue")
    },{
      path:"cart",
      component: ()=> import("../page/cart/cart.vue")
    },{
      path:"mine",
      component: ()=> import("../page/mine/mine.vue")
      }, {
      path: '',
      nredirect:"home"
    }]
  },{
    path: "/login",
    meta:{permission:true},
    component: ()=> import("../page/login/login.vue")
  },{
    path:"/register",
    component: ()=> import("../page/register/register.vue")
  },{
    path:"/goods",
    component: ()=> import("../page/goods/goods.vue")
  },{
    path:"/goodInfo",
    component: ()=> import("../page/goodInfo/goodInfo.vue")
  },{
    path: "/order/:id?",
    name:"order",
    component: () => import("../page/order/order.vue")
    },
  {
    path: "/mineOrder",
    component: () => import("../page/order/mineOrder.vue")
    },
    {
      path: "/address",
      component:()=>import("../page/address/address.vue")
    },
    {
      path: "/addressAdd",
      component:()=>import("../page/address/addressAdd.vue")
    },
    {
      path: '',
      redirect:'/login'
    },
    {
      path: "/404",
      component:()=>import('../page/404.vue')
    },
    {
    path:"*",
    redirect:"/404"
  }]
})

//全局前置守卫
// router.beforeEach((to, from, next) => {
//     if (to.path=="/login") {
//       next()
//       return
//   }
//   if (sessionStorage.getItem("user")) {
//     next()
//     return
//   }
//   next("/login")
// })
//全局后置守卫
// router.afterEach((to, from, next) => {
//   sessionStorage.setItem("person", 123);
// })

export default router